const path = require('path')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const ExtractPlugin = require('extract-text-webpack-plugin')
const webpack = require('webpack')
const dev = process.env.NODE_ENV === 'development'

const config = {
  entry: path.resolve(__dirname, '../src/index.js'),
  output: {
    filename: 'boundle.js',
    path: path.resolve(__dirname, '../dist')
  },
  module: {
    rules: [{
      test: /\.vue$/,
      loader: 'vue-loader'
    }, {
      test: /\.(gif|jpg|jpeg|png|svg)$/,
      use: [{
        loader: 'url-loader',
        options: {
          limit: 10240,
          name: '[name].[ext]'
        }
      }]
    }, {
      test: /\.jsx$/,
      loader: 'babel-loader'
    }]
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: dev ? '"development"' : '"production"'
      }
    }),
    new HTMLWebpackPlugin()
  ]
}

if (dev) {
  config.devServer = {
    port: '8000',
    host: '127.0.0.1',
    overlay: {
      error: true
    },
    hot: true
    // open: true
  }
  config.devtool = '#cheap-module-eval-source-map';
  config.module.rules.push({
    test: /\.css$/,
    // loader: 'css-loader'
    use: [
      'style-loader',
      'css-loader'
    ]
  }, {
    test: /\.scss/,
    use: [
      'style-loader',
      'css-loader',
      {
        loader: 'postcss-loader',
        options: {
          sourceMap: true
        }
      },
      'sass-loader'
    ]
  });
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  )
} else {
  config.output.filename = '[name].[chunkhash].js';
  config.module.rules.push({
    test: /\.css$/,
    use: ExtractPlugin.extract({
      fallback: 'style-loader',
      use: [
        'css-loader'
      ]
    })
  }, {
    test: /\.scss/,
    use: ExtractPlugin.extract({
      fallback: 'style-loader',
      use: [
        'css-loader',
        'sass-loader'
      ]
    })
  });
  config.plugins.push(
    new ExtractPlugin('styles.[contentHash].css')
  )
}

module.exports = config;